package helloworld.jiaxa.helloworld;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivityActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etPhone;
    private Button btnPhone;
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
    private Button btnSend;
    private EditText etMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_activity);

        //找到界面的按钮
        //点击按钮触发事件
        //        Button btnSubmit = (Button) findViewById(R.id.btnSubmit);  //alt + enter 弹出快捷菜单
        //第一种方法
        //        btnSubmit.setOnClickListener(new View.OnClickListener() {   //ctrl+q 弹出快捷提示
        //            @Override
        //            public void onClick(View v) {
        //                System.out.println("第一种方法");
        //                Toast.makeText(MainActivityActivity.this, "你点我干啥", Toast.LENGTH_SHORT).show();
        //            }
        //        });

        //第二种方法
        //        btnSubmit.setOnClickListener(new MyCLickListener());

        //第三种方法
        //        btnSubmit.setOnClickListener(this);

        //        etPhone = (EditText) findViewById(R.id.etPhone);   //ctrl + alt + f 字段转换为private字段
        //        btnPhone = (Button) findViewById(R.id.btnPhone);
        //        btnPhone.setOnClickListener(new PhoneClickListener());


        btnSend = (Button) findViewById(R.id.btnSend);
        etPhone = (EditText) findViewById(R.id.etPhone);
        etMessage = (EditText) findViewById(R.id.etMessage);

        btnSend.setOnClickListener(new sendMessageListener());
    }

    @Override
    public void onClick(View v) {
        System.out.println("第三种方法");
        Toast.makeText(this, "你点我干啥", Toast.LENGTH_SHORT).show();
    }

    //第二种点击事件
    class MyCLickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            System.out.println("第二种点击事件");
            Toast.makeText(MainActivityActivity.this, "你点我干啥", Toast.LENGTH_SHORT).show();
        }
    }

    void callPhone() {
        String phone = etPhone.getText().toString().trim();
        if ("".equals(phone)) {
            Toast.makeText(MainActivityActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(MainActivityActivity.this, phone, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();    //Intent.ACTION_DIAL：询问是否拨打；Intent.ACTION_CALL：直接拨打
        intent.setAction(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "18201081614");
        intent.setData(data);
        startActivity(intent);
    }

    //拨打电话按钮监听事件
    class PhoneClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            int hasPermission = ContextCompat.checkSelfPermission(MainActivityActivity.this, Manifest.permission.CALL_PHONE);
            System.out.println("权限：" + hasPermission);
            System.out.println("权限：" + PackageManager.PERMISSION_GRANTED);
            if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                System.out.println("权限：no");
                ActivityCompat.requestPermissions(MainActivityActivity.this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
            } else {
                System.out.println("权限：yes");
                callPhone();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        System.out.println("权限返回：" + requestCode);
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CALL_PHONE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission Granted
                    callPhone();
                } else { // Permission Denied
                    Toast.makeText(MainActivityActivity.this, "拨打电话权限未开启", Toast.LENGTH_SHORT).show();
                }
                break;
            default:

        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private class sendMessageListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String phone = etPhone.getText().toString().trim();
            String message = etMessage.getText().toString().trim();
            if ("".equals(phone) || "".equals(message)){
                Toast.makeText(MainActivityActivity.this,"电话或者内容不能为空",Toast.LENGTH_SHORT).show();
            }
            System.out.println("电话："+phone +"，内容："+message);
            SmsManager smsManager=SmsManager.getDefault();
            smsManager.sendTextMessage(phone,null,message,null,null);
        }
    }
}
